package com.shxzz.easy12306.framework.starter.idempotent.core;

import com.shxzz.easy12306.framework.starter.base.ApplicationContextHolder;
import com.shxzz.easy12306.framework.starter.idempotent.core.handler.IdempotentExecuteHandler;
import com.shxzz.easy12306.framework.starter.idempotent.core.handler.spel.IdempotentSpELByMQExecuteHandler;
import com.shxzz.easy12306.framework.starter.idempotent.core.handler.spel.IdempotentSpELByRestAPIExecuteHandler;
import com.shxzz.easy12306.framework.starter.idempotent.enums.IdempotentKeyType;
import com.shxzz.easy12306.framework.starter.idempotent.enums.IdempotentScene;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
 * 幂等逻辑处理器工厂
 * @author shxzz
 * @create 2024-02-22-22:21
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class IdempotentExecuteHandlerFactory {
    /**
     * 根据幂等场景与对应的幂等 key 类型获取幂等处理器实例
     * @param scene 幂等场景 {@link IdempotentScene}
     * @param idempotentKeyType 幂等 key 类型 {@link IdempotentKeyType}
     */
    public static IdempotentExecuteHandler getInstance(IdempotentScene scene, IdempotentKeyType idempotentKeyType) {
        IdempotentExecuteHandler handler = null;

        switch (scene) {
            case RESTAPI -> {
                switch (idempotentKeyType) {
                    case SPEL -> handler = ApplicationContextHolder.getBean(IdempotentSpELByRestAPIExecuteHandler.class);
                }
            }
            case MQ -> handler = ApplicationContextHolder.getBean(IdempotentSpELByMQExecuteHandler.class);
        }

        return handler;
    }
}
